package cn.tedu.mvc.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        System.out.println("当前时间为:"+hour);
        if(hour<15||hour>=22)
            throw new RuntimeException("不在访问时间范围内,请在15~22点之间访问");
        return true;//true放行，false表示拦截，请求不继续往下执行
    }
}
